<?php

class Admin_UserController extends ExtLb_Controller_Action
{
	private $userTbl;
	private $userFrm;
	private $auth;
	private $storageAuth;

    public function init()
    {
        $this->loadCss('admin', 'user', FOLDER_ROOT_NAME);
        $this->loadLayout('admin', 'layout');
        
        $this->userFrm	= new Admin_Form_User();
        $this->userTbl 	= new Admin_Model_Users();
        
        $this->auth 	=  Zend_Auth::getInstance();
        $this->storageAuth = $this->auth->getIdentity();
        date_default_timezone_set('Asia/Ho_Chi_Minh');
    }

    public function indexAction()
    {
    	if(isset($_GET['search_box_sumit']) && !empty($_GET['search_box_text'])){
    		$select = $this->userTbl->selectUsers("hoTen like '%".$_GET['search_box_text']."%'");
    	}else{
    		$select = $this->userTbl->selectUsers();
    	}
    	
        
        $paginatorUser = new Zend_Paginator(new Zend_Paginator_Adapter_DbSelect($select));
        $paginatorUser->setItemCountPerPage(20)
        			->setPageRange(3)
        			->setCurrentPageNumber($this->_getParam('page',1));
        			
        $this->view->paginatorUsers	= $paginatorUser;
        
        
	    ///
    	// === Move to action delete when del
    	if (isset($_GET['cmdDel'])){
    		$this->_forward('delete','user','admin');
    	}
    }
    
    public function addAction(){
    	//
    	//LOAD FORM AND DATA
    	if(isset($_GET['edit']) && !empty($_GET['user'])){
    		$this->userFrm->addAction('admin', 'user', 'edit');
    		$this->userFrm->addSubmitButton('cmdEdit', 'Cập nhập');
    		$this->view->titleForm = "Thay đổi thông tin";
    		
    		//LOAD DATA TO EDIT
    		$user = $this->userTbl->fetchRowUser($_GET['user']);
    		
    		$this->userFrm->getElement('txtUser')->setValue($user['tenDangNhap'])->setAttrib("readonly","readonly");
    		$this->userFrm->getElement('txtName')->setValue($user['hoTen']);
    		$this->userFrm->getElement('txtBirth')->setValue($user['ngaySinh']);
    		$this->userFrm->getElement('txtEmail')->setValue($user['email']);
    		$this->userFrm->getElement('cbbRole')->setValue($user['quyen']);
    	
    	}else {
    		$this->userFrm->addAction('admin', 'user', 'add');
    		$this->userFrm->addSubmitButton('cmdAdd', 'Thêm mới');
    		
    		$this->view->titleForm = "Thêm mới người dùng";
    	}
  	 	
    	//
    	//ADD
    	if (isset($_POST['cmdAdd'])) {
    		if ($this->userFrm->isValid($this->_request->getPost())) {
    			$tenDangNhap	= $this->_getParam('txtUser');
    			$matKhau		= $this->_getParam('txtPass');
    			$hoTen			= $this->_getParam('txtName');
    			$ngaySinh		= $this->_getParam('txtBirth');
    			$email			= $this->_getParam('txtEmail');
    			$quyen			= $this->_getParam('cbbRole');
    			
    			$now			= getdate();
    			$ngayTao		= $now['mday']."/".$now['mon']."/".$now['year'];
    			
//    			echo $tenDangNhap." ".$matKhau." ".$hoTen." ". $email. " " . $quyen." ". $ngayTao. " ". $ngaySinh;
				try {
					$this->userTbl->addUser($tenDangNhap, $matKhau, $quyen, $hoTen, $ngaySinh, $ngayTao, $email);
					$this->_forward('index','user','admin');
					$this->jDialogMessage("Thông báo","Thành công!!!");
												
				} catch (Exception $e) {
					$this->jDialogMessage("Thông báo","Lỗi! Có thể tên đăng nhập này đã tồn tại. Vui lòng thử tên khác!!!");
				}
    		}
    	}
    	$this->view->userFrm = $this->userFrm;
    	
	    ///
    	// === Move to action delete when del
    	if (isset($_GET['cmdDel'])){
    		$this->_forward('delete','user','admin');
    	}
    }

    public function editAction(){
    	if($this->userFrm->isValid($this->_request->getPost())){
    		if(isset($_POST['cmdEdit'])){
    			$tenDangNhap	= $this->_getParam('txtUser');
    			$matKhau		= $this->_getParam('txtPass');
    			$hoTen			= $this->_getParam('txtName');
    			$ngaySinh		= $this->_getParam('txtBirth');
    			$email			= $this->_getParam('txtEmail');
    			$quyen			= $this->_getParam('cbbRole');
    			
    			$now			= getdate();
    			$ngayTao		= $now['mday']."/".$now['mon']."/".$now['year'];
    			
    			$data = array(
    				'matKhau'	=> $matKhau,
    				'hoTen'		=> $hoTen,
    				'ngaySinh'	=> $ngaySinh,
    				'email'		=> $email,
    				'quyen'		=> $quyen,
    			);
    			
    			try {
    				$this->userTbl->updateCustomUser($data, "tenDangNhap ='".$tenDangNhap."'");
    			
    				$this->jDialogMessage("Thông báo", "Thành công!!!");	
    			} catch (Exception $e) {
    				$this->jDialogMessage("Thông báo","Lỗi! Đã có lỗi xảy ra trong quá trình xử lý. Vui lòng thử lại!!!");
    			}
    			
    			$this->_forward('index','user','admin');
    			
    		}
    	}
		
    	///
    	// === Move to action delete when del
    	if (isset($_GET['cmdDel'])){
    		$this->_forward('delete','user','admin');
    	}
    }

    public function infoAction(){
    	
    	// === LOAD DATA TO EDIT
    	$user = $this->userTbl->fetchRowUser($this->storageAuth->tenDangNhap);
    	
    	$this->userFrm->getElement('txtUser')->setValue($user['tenDangNhap'])->setAttrib("readonly","readonly");
    	$this->userFrm->getElement('txtName')->setValue($user['hoTen']);
    	$this->userFrm->getElement('txtBirth')->setValue($user['ngaySinh']);
    	$this->userFrm->getElement('txtEmail')->setValue($user['email']);
    	$this->userFrm->getElement('cbbRole')->setValue($user['quyen']);
    		
    	$this->view->userFrm = $this->userFrm;
    	$this->view->titleUser = $user['hoTen'];
    }
    
    public function deleteAction(){
    	//
    	// === one Del
    	if(isset($_GET['delete']) && isset($_GET['user']) && !empty($_GET['user'])){
    		try {
    			$this->userTbl->deleteUser($_GET['user']);
    			$this->jDialogMessage("Thông báo", "Đã xóa!!!");
    			
    		} catch (Exception $e) {
    			$this->jDialogMessage("Lỗi", "Đã có lỗi xảy ra trong quá trình xử lý. Vui lòng thử lại!!!");
    		}

    		$this->_forward('index','user','admin');
    		
    	}elseif (isset($_GET['cmdDel']) && isset($_GET['user-check']) && !empty($_GET['user-check'])){ // === Multi Del
    		
    		try {
	    		$arrUser = $_GET['user-check'];
	    		for ($i=0; $i < count($arrUser); $i++){
	    			$this->userTbl->deleteUser($arrUser[$i]);
	    		}	
    		}catch (Exception $e){
    			$this->jDialogMessage("Lỗi", "Đã có lỗi xảy ra trong quá trình xử lý. Vui lòng thử lại!!!");
    		}
    		
    		$this->_redirect('admin/user/');
    	}else{
    		$this->_redirect('admin/user/');
    	}
    
    }
}

